using System;
using System.Collections.Generic;
using System.Text;

using NSimpleDB.Service.Contract;
//using NSimpleDB.Service.VistaDb;

namespace NSimpleDB.BlogDemo
{
    class Program
    {
        static void Main()
        {
            ISimpleDBService ts;
            ts = new NSimpleDB.Service.Amazon.AmazonSimpleDBService("...", "...");
            ts.CreateDomain("mydomain");

            ISimpleDBDashbord db = (ts as ISimpleDBDashbord);
            Console.WriteLine("request id: {0}", db.LastResponseMetaData.RequestId);
            Console.WriteLine("box usage: {0}", db.LastResponseMetaData.BoxUsage);

            ts.PutAttributes("mydomain", "1",
                                    new SimpleDBAttribute("name", "peter"), 
                                    new SimpleDBAttribute("city", "london"));
            ts.PutAttributes("mydomain", "2", 
                                    new SimpleDBAttribute("name", "paul"), 
                                    new SimpleDBAttribute("city", "berlin"));
            ts.PutAttributes("mydomain", "3", 
                                    new SimpleDBAttribute("name", "mary"), 
                                    new SimpleDBAttribute("city", "london"));

            string nextToken = null;
            string[] itemNames;
            itemNames = ts.Query("mydomain", "['city'='london']", ref nextToken);
            foreach (string itemName in itemNames)
            {
                Console.WriteLine("item: {0}", itemName);
                ISimpleDBAttribute[] attributes;
                attributes = ts.GetAttributes("mydomain", itemName);
                foreach (ISimpleDBAttribute attr in attributes)
                    Console.WriteLine("  {0}={1}", attr.Name, attr.Value);
            }
            ts.DeleteDomain("mydomain");
            ts.Close();
        }


        static void MainOld(string[] args)
        {
            System.IO.File.Delete("hello.ts");

            using (ISimpleDBService ts = new NSimpleDB.Service.VistaDb.VistaDbSimpleDBService("hello.vdb3"))
            {
                ts.CreateDomain("contacts");

                ts.PutAttributes("contacts", 
                                 "123",
                                 new SimpleDBAttribute("Firstname", "John"),
                                 new SimpleDBAttribute("Lastname", "Doe"));

                ts.PutAttributes("contacts",
                                 "999",
                                 new SimpleDBAttribute("Firstname", "Peter"),
                                 new SimpleDBAttribute("Lastname", "Doe"));

                ts.PutAttributes("contacts",
                                 "482",
                                 new SimpleDBAttribute("Firstname", "John"),
                                 new SimpleDBAttribute("Lastname", "Sellinger"));

                ts.PutAttributes("contacts",
                                 "732",
                                 new SimpleDBAttribute("Firstname", "Paul"),
                                 new SimpleDBAttribute("Lastname", "Davis"));

                string[] itemNames;
                string nextToken = null;
                string query = "['Firstname'='John' OR 'Firstname'='Peter'] UNION ['Lastname'='Davis']";
                itemNames = ts.Query("contacts", 
                                     query,
                                     2,
                                     ref nextToken);
                while (itemNames.Length > 0)
                {
                    foreach (string itemName in itemNames)
                    {
                        Console.WriteLine(itemName);
                        ISimpleDBAttribute[] attributes;
                        attributes = ts.GetAttributes("contacts", itemName);
                        foreach (ISimpleDBAttribute a in attributes)
                            Console.WriteLine("{0}={1}", a.Name, a.Value);
                    }

                    itemNames = ts.Query("contacts", query, ref nextToken);
                }
            }
        }
    }
}
